package com.meow_woof.utils;

import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Component
public class BloomFilterHelper {

    @Resource
    private RedissonClient redissonClient;

    private RBloomFilter<Long> shopBloomFilter;

    @PostConstruct
    public void init() {
        // 初始化布隆过滤器，名字叫 bf:shop:id
        shopBloomFilter = redissonClient.getBloomFilter("bf:shop:id");
        // 初始化：预期元素数量 100万，误判率 0.001
        shopBloomFilter.tryInit(1_000_000L, 0.001);
    }

    public void add(Long id) {
        shopBloomFilter.add(id);
    }

    public boolean mightContain(Long id) {
        return shopBloomFilter.contains(id);
    }
}

